<template>
    <div class="bg-img-container">
        <page-main title="新增背景图">
            <el-row>
                <el-col :span="24" class="title-text">标题:</el-col>
                <el-col>
                    <el-input
                        v-model="title"
                        class="title-input"
                        placeholder="请输入标题内容"
                    />
                </el-col>
            </el-row>
            <el-row class="upload-img">
                <el-col :span="24" class="title-text">背景图:</el-col>
                <el-col>
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:8888/api/uploadAction"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon" />
                    </el-upload>
                </el-col>
            </el-row>
            <el-row>
                <el-button type="primary" @click="addBgImg">保存</el-button>
            </el-row>
        </page-main>
    </div>
</template>
<script>
import { upLoadImg, addBgImg } from '@/api/myIndex'

export default {
    name: 'BgImg',
    data() {
        return {
            title: '',
            // 用于回显的图片url
            imageUrl: '',
            // 上传到服务器的图片路径
            imgPath: ''
        }
    },
    methods: {
        handleAvatarSuccess(res, file) {
            console.log(file)
            this.imageUrl = URL.createObjectURL(file.raw)
        },
        beforeAvatarUpload(file) {
            let fd = new FormData()
            fd.append('file', file)
            upLoadImg(fd)
                .then(val => {
                    this.imgPath = val.data
                    this.$message({
                        type: 'success',
                        message: '上传成功'
                    })
                })
                .catch(error => {
                    this.$message.error(error)
                })

            return true
        },
        // 新增背景图确认
        async addBgImg() {
            let title = this.title
            let imgPath = this.imgPath
            if (!title.trim() || !imgPath) {
                this.$message.error('标题或图片不能为空')
                return
            }
            let result = await addBgImg(title, imgPath)
            if (result.code === 200) {
                this.$message({
                    type: 'success',
                    message: '保存成功'
                })
                this.imageUrl = ''
                this.title = ''
                return
            }
            this.$message.error(result.message)

        }
    }
}
</script>
<style lang="scss" scoped>
.editor-container ::v-deep .actionbar {
    z-index: 99;
}
.preview {
    margin-top: 10px;
    &::before {
        content: "预览：";
        display: block;
    }
}
.title-text {
    font-size: 20px;
    font-weight: 600;
}
.title-input {
    margin: 20px 0;
}
// .upload-img ::v-deep .el-upload {
//     margin: 20px 0;
// }
.avatar-uploader ::v-deep .el-upload {
    margin: 20px 0;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
